package org.shock.ss.sms.dao.system.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.shock.ss.sms.bean.system.Dic;
import org.shock.ss.sms.commons.db.ProxoolDbPool;
import org.shock.ss.sms.commons.dao.impl.BaseDaoImpl;
import org.shock.ss.sms.dao.system.DicDao;

public class DicDaoImpl extends BaseDaoImpl implements DicDao {
	/**
	 * 根据传入的数组查询数据字典的信息，返回一个与数组对应的Map
	 * 
	 * @param s
	 * @return
	 */
	public Map<String, List<Map<String, String>>> getDataDictionaryMap(
			String[] s) throws Exception {
		Map<String, List<Map<String, String>>> map = new TreeMap<String, List<Map<String, String>>>();
		List<List<Map<String, String>>> lists = new ArrayList<List<Map<String, String>>>();
		String addString = "";
		for (int i = 0; i < s.length; i++) {
			addString += (addString.equals("") ? "" : ",") + "'" + s[i] + "'";
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			lists.add(list);
		}

		Map<String, String> allMap = new HashMap<String, String>();
		if (!"".equals(addString)) {
			String sql = "select id,pcode,value from "+Dic.getTable()+" where pcode in ("
					+ addString + ") order by paixu,id";
			Connection conn = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				conn = ProxoolDbPool.getConnection();
				stmt = conn.prepareStatement(sql);
				rs = stmt.executeQuery();
				while (rs.next()) {
					Map<String, String> map1 = new LinkedHashMap<String, String>();
					map1.put("key", rs.getString("id"));
					map1.put("value", rs.getString("value"));
					for (int i = 0; i < s.length; i++) {
						if (rs.getString("pcode").equals(s[i])) {
							lists.get(i).add(map1);
						}
					}
					allMap.put(rs.getString("id"), rs.getString("value"));
				}
				for (int i = 0; i < s.length; i++) {
					map.put(s[i], lists.get(i));
				}
				List<Map<String, String>> allList = new ArrayList<Map<String, String>>();
				allList.add(allMap);
				map.put("ALLDICTIONARY", allList);
			} catch (Exception e) {
				log.error(e,e);
				throw e;
			} finally {
				ProxoolDbPool.closeResultSet(rs);
				ProxoolDbPool.closeStatement(stmt);
				ProxoolDbPool.closeConnection(conn);
			}
		}
		return map;
	}
	
	public Map<String, List<Map<String, String>>> getDataDictionaryMap2(
			String[] s) throws Exception {
		Map<String, List<Map<String, String>>> map = new TreeMap<String, List<Map<String, String>>>();
		List<List<Map<String, String>>> lists = new ArrayList<List<Map<String, String>>>();
		String addString = "";
		for (int i = 0; i < s.length; i++) {
			addString += (addString.equals("") ? "" : ",") + "'" + s[i] + "'";
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			lists.add(list);
		}

		Map<String, String> allMap = new HashMap<String, String>();
		if (!"".equals(addString)) {
			String sql = "select code,pcode,value from "+Dic.getTable()+" where pcode in ("
					+ addString + ") order by code";
			Connection conn = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				conn = ProxoolDbPool.getConnection();
				stmt = conn.prepareStatement(sql);
				rs = stmt.executeQuery();
				while (rs.next()) {
					Map<String, String> map1 = new LinkedHashMap<String, String>();
					map1.put("key", rs.getString("code"));
					map1.put("value", rs.getString("value"));
					for (int i = 0; i < s.length; i++) {
						if (rs.getString("pcode").equals(s[i])) {
							lists.get(i).add(map1);
						}
					}
					allMap.put(rs.getString("code"), rs.getString("value"));
				}
				for (int i = 0; i < s.length; i++) {
					map.put(s[i], lists.get(i));
				}
				List<Map<String, String>> allList = new ArrayList<Map<String, String>>();
				allList.add(allMap);
				map.put("ALLDICTIONARY", allList);
			} catch (Exception e) {
				log.error(e,e);
				throw e;
			} finally {
				ProxoolDbPool.closeResultSet(rs);
				ProxoolDbPool.closeStatement(stmt);
				ProxoolDbPool.closeConnection(conn);
			}
		}
		return map;
	}
}
